$(function(){
    var jd_jingxuan_box = new Vue({
        el: "#jd_jingxuan",
        data: {
            token: '',
            cid1: -1,
            cid2: -1,
            cid3: -1,
            keyword: '',
            category_1_list: [],
            category_2_list: [],
            category_3_list: [],
            all_category_2_list: [],
            all_category_3_list: [],

            datalist: [],           // 分页固定参数名
            page_num: 1,
            page_size: 25,
            total: 0,
            page_total: [],
            page_total_num: 0
        },
        mounted: function() {
            this.initialPage();
            this.getDataList();
            this.getCategoryList();
        },
        watch: {// 数据变化事件
            cid1: function (newVal, oldVal) {
                if (newVal != oldVal) {
                    this.cid2 = -1;
                    this.cid3 = -1;

                    if (newVal != -1) {
                        this.getCategoryList(1, newVal);
                    }
                }
            },
            cid2: function (newVal, oldVal) {
                if (newVal != oldVal) {
                    this.cid3 = -1;
                    if (newVal != -1) {
                        this.getCategoryList(2, newVal);
                    }
                }
            },
        },
        methods: {
            initialPage: function () {
                this.token = $('#lcm_lingpai').val();
                // 设置弹窗
                toastr.options = {
                    "closeButton": true, // 是否显示关闭按钮
                    "debug": false,       // 是否使用debug模式
                    "positionClass": "toast-top-center",// 弹出窗的位置
                    "showDuration": "100", // 显示的动画时间
                    "hideDuration": "100",// 消失的动画时间
                    "timeOut": "1000",     // 展现时间
                    "extendedTimeOut": "100",// 加长展示时间
                    "showEasing": "swing",    // 显示时的动画缓冲方式
                    "hideEasing": "linear",   // 消失时的动画缓冲方式
                    "showMethod": "fadeIn",   // 显示时的动画方式
                    "hideMethod": "fadeOut"   // 消失时的动画方式
                };
            },
            nextPage: function(page) {// 页面跳转
                if (this.page_num == page || page < 1 || page > this.page_total_num) {
                    return false;
                }
                this.page_num = page;
                this.getDataList();
            },
            // 获取类别列表
            getCategoryList: function (grade = 0, parent_id = 0) {
                if (grade == 1) {
                    if (this.all_category_2_list[parent_id] != undefined) {
                        this.category_2_list = this.all_category_2_list[parent_id];
                        return false;
                    }
                }

                if (grade == 2) {
                    if (this.all_category_3_list[parent_id] != undefined) {
                        this.category_3_list = this.all_category_3_list[parent_id];
                        return false;
                    }
                }

                $.ajax({
                    method: 'POST',
                    url: '/backend/jd/categorylistpost',
                    data: {
                        _token: this.token,
                        category_grade: grade,
                        parent_id: parent_id,
                        page_num: 1,
                        page_size: 300
                    },
                    dataType: 'json',
                    success:(res)=>{
                        if (res.code == 200) {
                            switch (grade) {
                                case 0:
                                    jd_jingxuan_box.category_1_list = res.data.list;
                                    break;
                                case 1:
                                    jd_jingxuan_box.all_category_2_list[parent_id] = res.data.list;
                                    jd_jingxuan_box.category_2_list = res.data.list;
                                    break;
                                case 2:
                                    jd_jingxuan_box.all_category_3_list[parent_id] = res.data.list;
                                    jd_jingxuan_box.category_3_list = res.data.list;
                                    break;
                            }
                        } else {
                            toastr.error(res.message);
                        }
                    }
                })
            },
            // 获取数据
            getDataList: function () {
                $.ajax({
                    method: 'POST',
                    url: '/backend/jd/jingxuanpost',
                    data: {
                        _token: this.token,
                        keyword: this.keyword,
                        cid1: this.cid1,
                        cid2: this.cid2,
                        cid3: this.cid3,

                        page_num: this.page_num,
                        page_size: this.page_size
                    },
                    dataType: 'json',
                    success:(res)=>{
                        if (res.code == 200) {
                            toastr.success(res.message);
                            jd_jingxuan_box.datalist = [];
                            var z = k = 0;
                            for (z; z < res.data.list.length; z++) {
                                if (jd_jingxuan_box.datalist[k] != undefined && jd_jingxuan_box.datalist[k].length == 5) {
                                    k++;
                                }

                                if (jd_jingxuan_box.datalist[k] == undefined) {
                                    jd_jingxuan_box.datalist.push([]);
                                }

                                jd_jingxuan_box.datalist[k].push(res.data.list[z]);
                            }

                            jd_jingxuan_box.total = res.data.total;
                            // 计算分页按钮
                            var total_page = (jd_jingxuan_box.total/jd_jingxuan_box.page_size).toFixed(0);
                            jd_jingxuan_box.page_total_num = total_page;
                            var next_page = 0;
                            jd_jingxuan_box.page_total = [jd_jingxuan_box.page_num];
                            for (var i = 1; i <= 4; i++) {// 右边补入按钮
                                next_page = jd_jingxuan_box.page_num + i;
                                if (next_page <= total_page) {
                                    jd_jingxuan_box.page_total.push(next_page)
                                }
                            }

                            if (jd_jingxuan_box.page_total.length < 5) {// 左边补入按钮
                                for (i = 1; i <= 4; i++) {              // 左边补入按钮
                                    next_page = jd_jingxuan_box.page_num - i;
                                    if (jd_jingxuan_box.page_total.length < 5 && next_page > 0) {
                                        jd_jingxuan_box.page_total.unshift(next_page)
                                    }
                                }
                            }
                        } else {
                            toastr.error(res.message);
                        }
                    }
                })
            }
        }
    });
});
